package com.edu.cuotibook.dao;

import com.edu.cuotibook.dao.mapper.ExaminationDao;
import com.edu.cuotibook.entity.common.CommonException;
import com.edu.cuotibook.entity.pojo.Examination;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ExaminationDaoService{

    @Resource
    private ExaminationDao examinationDao;

    public int insert(Examination pojo){
        return examinationDao.insert(pojo);
    }

    public int insertSelective(Examination pojo){
        return examinationDao.insertSelective(pojo);
    }

    public int insertList(List<Examination> pojos){
        return examinationDao.insertList(pojos);
    }

    public int update(Examination pojo){
        return examinationDao.update(pojo);
    }

    public void insertOrUpdate(Examination examination) {
        List<Examination> result = examinationDao.select(examination);
        if (!CollectionUtils.isEmpty(result)) {
//            examination.setId(result.get(0).getId());
//            return;
            throw new CommonException("【考试《"+examination.getTile()+"》已经录入】");
        }
        examinationDao.insertSelective(examination);
    }

    public Examination selectById(Long id) {
        return examinationDao.selectById(id);
    }

    public PageInfo select(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Examination> select = examinationDao.select(new Examination());
        return PageInfo.of(select);
    }

    public void deleteById(Long id) {
        examinationDao.deleteById(id);
    }
}
